﻿using System;
using System.Text;

namespace RuoVea.ExUtil;

/// <summary>
/// 描 述：日期时间扩展
/// </summary>
public static partial class Extensions
{
    /// <summary>
    /// 获取格式化字符串，带时分秒，格式："yyyy-MM-dd HH:mm:ss"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <param name="isRemoveSecond">是否移除秒</param>
    /// <returns>格式化字符串，带时分秒，格式:yyyy-MM-dd HH:mm:ss</returns>
    public static string ToDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
    {
        if (isRemoveSecond)
            return dateTime.ToString("yyyy-MM-dd HH:mm");
        return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    /// 获取格式化字符串，带时分秒，格式："yyyy-MM-dd HH:mm:ss"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <param name="isRemoveSecond">是否移除秒</param>
    /// <returns>格式化字符串，带时分秒，格式:yyyy-MM-dd HH:mm:ss</returns>
    public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
    {
        if (dateTime == null)
            return string.Empty;
        return ToDateTimeString(dateTime.Value, isRemoveSecond);
    }

    /// <summary>
    /// 获取格式化字符串，不带时分秒，格式："yyyy-MM-dd"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，带时分秒，格式:yyyy-MM-dd</returns>
    public static string ToDateString(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// 获取格式化字符串，不带时分秒，格式：yyyy-MM-dd
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，带时分秒，格式:yyyy-MM-dd</returns>
    public static string ToDateString(this DateTime? dateTime)
    {
        if (dateTime == null)
            return string.Empty;
        return ToDateString(dateTime.Value);
    }

    /// <summary>
    /// 获取格式化字符串，不带年月日，格式：HH:mm:ss
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，带时分秒，格式:HH:mm:ss</returns>
    public static string ToTimeString(this DateTime dateTime)
    {
        return dateTime.ToString("HH:mm:ss");
    }

    /// <summary>
    /// 获取格式化字符串，不带年月日，格式：HH:mm:ss
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，带时分秒，格式:HH:mm:ss</returns>
    public static string ToTimeString(this DateTime? dateTime)
    {
        if (dateTime == null)
            return string.Empty;
        return ToTimeString(dateTime.Value);
    }

    /// <summary>
    /// 获取格式化字符串，带毫秒，格式：yyyy-MM-dd HH:mm:ss.fff
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>获取格式化字符串，带毫秒，格式：yyyy-MM-dd HH:mm:ss.fff</returns>
    public static string ToMillisecondString(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
    }

    /// <summary>
    /// 获取格式化字符串，带毫秒，格式："yyyy-MM-dd HH:mm:ss.fff"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，带毫秒，格式：yyyy-MM-dd HH:mm:ss.fff</returns>
    public static string ToMillisecondString(this DateTime? dateTime)
    {
        if (dateTime == null)
            return string.Empty;
        return ToMillisecondString(dateTime.Value);
    }

    /// <summary>
    /// 获取格式化字符串，不带时分秒，格式：yyyy年MM月dd日
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，不带时分秒，格式：yyyy年MM月dd日</returns>
    public static string ToChineseDateString(this DateTime dateTime)
    {
        return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
    }

    /// <summary>
    /// 获取格式化字符串，不带时分秒，格式："yyyy年MM月dd日"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <returns>格式化字符串，不带时分秒，格式："yyyy年MM月dd日"</returns>
    public static string ToChineseDateString(this DateTime? dateTime)
    {
        if (dateTime == null)
            return string.Empty;
        return ToChineseDateString(dateTime.SafeValue());
    }

    /// <summary>
    /// 获取格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <param name="isRemoveSecond">是否移除秒</param>
    /// <returns>格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"</returns>
    public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
    {
        StringBuilder result = new StringBuilder();
        result.AppendFormat("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
        result.AppendFormat(" {0}时{1}分", dateTime.Hour, dateTime.Minute);
        if (isRemoveSecond == false)
            result.AppendFormat("{0}秒", dateTime.Second);
        return result.ToString();
    }

    /// <summary>
    /// 获取格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"
    /// </summary>
    /// <param name="dateTime">日期</param>
    /// <param name="isRemoveSecond">是否移除秒</param>
    /// <returns>格式化字符串，带时分秒，格式："yyyy年MM月dd日 HH时mm分"</returns>
    public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
    {
        if (dateTime == null)
            return string.Empty;
        return ToChineseDateTimeString(dateTime.Value);
    }

    #region 时间处理
    private static readonly DateTime dateStart = new(1970, 1, 1, 8, 0, 0);

    private static readonly long longTime = 621355968000000000;

    private static readonly int samllTime = 10000000;

    /// <summary>
    /// 获取时间戳 
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static long GetTimeStamp(this DateTime dateTime)
    {
        return (dateTime.ToUniversalTime().Ticks - longTime) / samllTime;
    }

    /// <summary>
    /// 时间戳转换成日期
    /// </summary>
    /// <param name="timeStamp"></param>
    /// <returns></returns>
    public static DateTime GetTimeSpmpToDate(this object timeStamp)
    {
        if (timeStamp == null) return dateStart;
        DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
        return dateTime;
    }
    #endregion
}
